using System;
using System.Data;
using System.Collections;
using MyFramework;
using MyFramework.Data;
using MIMS.WRS.Entities;
using MIMS.WRS.Data;
    
namespace MIMS.WRS.Business
{
	/// <summary>
	/// Summary description for ShoppingListService.
	/// </summary>
	public class ShoppingListService
	{
		#region "Shopping Order & list"

		public static bool CreateShoppingCartList(ShoppingListCollection shoppingList, ShoppingOrder  shopingOrder, int shoppingUserId,int customerId)
		{            
			bool bReturned = false;
			try
			{
				ShoppingListDAO shoppingListDAO = new ShoppingListDAO();
				shoppingListDAO.CreateShoppingOrderList(shoppingList,shopingOrder,shoppingUserId, customerId);
				bReturned= true;
			}
			catch (BusinessException)
			{
				throw;
			}
			catch (Exception ex)
			{
				// log this exception
				Logger.Error(ex);
				// wrap it and rethrow
				throw new BusinessException(SR.BusinessCreateShoppingListException, ex);
			}
			return bReturned;
		}    

		public static bool CreateShoppingOrderListToExistedList(ShoppingListCollection shoppingList, ShoppingOrder  shopingOrder, int shoppingUserId,int customerId)
		{            
			bool bReturned = false;
			try
			{
				ShoppingListDAO shoppingListDAO = new ShoppingListDAO();
				shoppingListDAO.CreateShoppingOrderListToExistedList(shoppingList,shopingOrder,shoppingUserId, customerId);
				bReturned= true;
			}
			catch (BusinessException)
			{
				throw;
			}
			catch (Exception ex)
			{
				// log this exception
				Logger.Error(ex);
				// wrap it and rethrow
				throw new BusinessException(SR.BusinessCreateShoppingListException, ex);
			}
			return bReturned;
		}  

		public static bool DeleteShoppingOrderList( int shoppingUserId,int conPointId)
		{            
			try
			{
				ShoppingListDAO shoppingListDAO = new ShoppingListDAO();
				shoppingListDAO.DeleteShoppingOrderList(shoppingUserId,conPointId);
				return true;
			}
			catch (BusinessException)
			{
				throw;
			}
			catch (Exception ex)
			{
				// log this exception
				Logger.Error(ex);
				// wrap it and rethrow
				throw new BusinessException("SR.DeleteShoppingOrderListException", ex);
			}
			return false;
		}    

		public static bool DeleteFullShoppingOrderById(int shoppingOrderId)
		{            
			try
			{
				ShoppingListDAO shoppingListDAO = new ShoppingListDAO();
				shoppingListDAO.DeleteFullShoppingOrderById(shoppingOrderId);
				return true;
			}
			catch (BusinessException)
			{
				throw;
			}
			catch (Exception ex)
			{
				// log this exception
				Logger.Error(ex);
				// wrap it and rethrow
				throw new BusinessException("BusinessDeleteFullShoppingOrderByIdException", ex);
			}
			return false;
		}    
		
		public static ShoppingCustomerOrderCollection GetShoppingCustomerOrderCollection()
		{
			ShoppingCustomerOrderCollection shoppingList = new ShoppingCustomerOrderCollection();
			try
			{
				ShoppingCustomerDAO shoppingCustomerDAO = new ShoppingCustomerDAO();
				shoppingList = shoppingCustomerDAO.GetShoppingCustomerOrderCollection();
			}
			catch (BusinessException)
			{
				throw;
			}
			catch (Exception ex)
			{
				// log this exception
				Logger.Error(ex);
				// wrap it and rethrow
				throw new BusinessException("BusinessGetShoppingCustomerOrderCollectionException", ex);
			}
			return shoppingList;
		}
		

		public static IDataReader GetShoppingCustomerForReport()
		{            
			try
			{
				ShoppingCustomerDAO shoppingCustomerDAO = new ShoppingCustomerDAO();
				return shoppingCustomerDAO.GetShoppingCustomerForReport();
			}
			catch (Exception ex)
			{
				// log this exception
				Logger.Error(ex);
				// wrap it and rethrow
				throw new BusinessException("BusinessGetShoppingCustomerForReportException", ex);
			}
		}   

		public static IDataReader GetShoppingCustomerForReportByShoppingOrderId(int shoppingOrderId)
		{            
			try
			{
				ShoppingCustomerDAO shoppingCustomerDAO = new ShoppingCustomerDAO();
				return shoppingCustomerDAO.GetShoppingCustomerForReportByShoppingOrderId(shoppingOrderId);
			}
			catch (Exception ex)
			{
				// log this exception
				Logger.Error(ex);
				// wrap it and rethrow
				throw new BusinessException("BusinessGetShoppingCustomerForReportByShoppingOrderIdException", ex);
			}
		}   

		public static ShoppingListCollection GetShoppingListByShoppingUserIdAndConPoint(int shoppingUserId, int conPointId)
		{
			ShoppingListCollection shoppingList = new ShoppingListCollection();
			try
			{
				ShoppingListDAO shoppingListDAO = new ShoppingListDAO();
				shoppingList = shoppingListDAO.GetShoppingListByShoppingUserIdAndConPoint(shoppingUserId,conPointId);
			}
			catch (BusinessException)
			{
				throw;
			}
			catch (Exception ex)
			{
				// log this exception
				Logger.Error(ex);
				// wrap it and rethrow
				throw new BusinessException("BusinessGetShoppingListByShoppingUserIdException", ex);
			}
			return shoppingList;
		}

		public static ShoppingListCollection GetShoppingListByShoppingOrderId(int shoppingUserId)
		{
			ShoppingListCollection shoppingList = new ShoppingListCollection();
			try
			{
				ShoppingListDAO shoppingListDAO = new ShoppingListDAO();
				shoppingList = shoppingListDAO.GetShoppingListByShoppingOrderId(shoppingUserId);
			}
			catch (BusinessException)
			{
				throw;
			}
			catch (Exception ex)
			{
				// log this exception
				Logger.Error(ex);
				// wrap it and rethrow
				throw new BusinessException("BusinessGetShoppingListByShoppingUserIdException", ex);
			}
			return shoppingList;
		}

		public static IDataReader GetShoppingListByShoppingOrderIdReport(int shoppingUserId)
		{
			try
			{
				ShoppingListDAO shoppingListDAO = new ShoppingListDAO();
				return shoppingListDAO.GetShoppingListByShoppingOrderIdReport(shoppingUserId);
			}
			catch (Exception ex)
			{
				// log this exception
				Logger.Error(ex);
				// wrap it and rethrow
				throw new BusinessException("BusinessGetShoppingListByShoppingOrderIdReportException", ex);
			}
			
		}
		
		public static ShoppingOrderCollection GetShoppingOrderByShoppingUserId(int shoppingUserId)
		{
			ShoppingOrderCollection shoppingOrderCol = new ShoppingOrderCollection();
			try
			{
				ShoppingOrderDAO shoppingOrderDAO = new ShoppingOrderDAO();
				shoppingOrderCol = shoppingOrderDAO.GetShoppingOrderByUserId(shoppingUserId);

			}
			catch (BusinessException)
			{
				throw;
			}
			catch (Exception ex)
			{
				// log this exception
				Logger.Error(ex);
				// wrap it and rethrow
				throw new BusinessException("BusinessGetShoppingOrderByShoppingUserIdException", ex);
			}
			return shoppingOrderCol;
		}
		public static void UpdateShoppingListItem(ShoppingList shoppingListItem)
		{            
			try
			{
				ShoppingListDAO shoppingListDAO = new ShoppingListDAO();
				shoppingListDAO.UpdateShoppingList(shoppingListItem);
			}
			catch (BusinessException)
			{
				throw;
			}
			catch (Exception ex)
			{
				// log this exception
				Logger.Error(ex);
				// wrap it and rethrow
				throw new BusinessException(SR.BusinessUpdateShoppingListException, ex);
			}
		}  

		public static void DeleteShoppingListItem(int shoppingListItemId)
		{            
			try
			{
				ShoppingListDAO shoppingListDAO = new ShoppingListDAO();
				shoppingListDAO.DeleteShoppingList(shoppingListItemId);
			}
			catch (BusinessException)
			{
				throw;
			}
			catch (Exception ex)
			{
				// log this exception
				Logger.Error(ex);
				// wrap it and rethrow
				throw new BusinessException(SR.BusinessDeleteShoppingListException, ex);
			}
		}  


		public static int CreateShoppingOrder(ShoppingOrder shoppingOrder)
		{            
			try
			{
				ShoppingOrderDAO shoppingOrderDAO = new ShoppingOrderDAO();
				shoppingOrderDAO.CreateShoppingOrder(shoppingOrder);
			}
			catch (BusinessException)
			{
				throw;
			}
			catch (Exception ex)
			{
				// log this exception
				Logger.Error(ex);
				// wrap it and rethrow
				throw new BusinessException(SR.BusinessCreateShoppingOrderException, ex);
			}
			return shoppingOrder.ShoppingOrderId;
		}    

		public static void UpdateShoppingOrder(ShoppingOrder shoppingOrder)
		{            
			try
			{
				ShoppingOrderDAO shoppingOrderDAO = new ShoppingOrderDAO();
				shoppingOrderDAO.UpdateShoppingOrder(shoppingOrder);
			}
			catch (BusinessException)
			{
				throw;
			}
			catch (Exception ex)
			{
				// log this exception
				Logger.Error(ex);
				// wrap it and rethrow
				throw new BusinessException(SR.BusinessUpdateShoppingOrderException, ex);
			}
		}  

		public static void DeleteShoppingOrder(int shoppingOrderId)
		{            
			try
			{
				ShoppingOrderDAO shoppingOrderDAO = new ShoppingOrderDAO();
				shoppingOrderDAO.DeleteShoppingOrder(shoppingOrderId);
			}
			catch (BusinessException)
			{
				throw;
			}
			catch (Exception ex)
			{
				// log this exception
				Logger.Error(ex);
				// wrap it and rethrow
				throw new BusinessException(SR.BusinessDeleteShoppingOrderException, ex);
			}
		}  

		public static bool IsExistedOrder(int shoppingUserId)
		{            
			try
			{
				ShoppingOrderDAO shoppingOrderDAO = new ShoppingOrderDAO();
				return shoppingOrderDAO.IsExistedShoppingOrder(shoppingUserId);
			}
			catch (BusinessException)
			{
				throw;
			}
			catch (Exception ex)
			{
				// log this exception
				Logger.Error(ex);
				// wrap it and rethrow
				throw new BusinessException("BusinessIsExistedOrderException", ex);
			}
			return false;
		}  

		#endregion

		#region "Shopping User"

		public static int Login(string email, string pwd) 
		{
			ShoppingUser user = GetUSER(email);
			if (user == null)
				throw new ApplicationException("Invalid email or the email has not registered yet. ");
			if (user.Password.Equals(pwd) == false)
				throw new ApplicationException("Invalid password");
			return user.UserId;
		}

		public static bool IsExistedEmail(string email) 
		{
			ShoppingUser user = GetUSER(email);
			if (user != null && user.UserId > 0)
				return true;
			return false;
		}
		public static ShoppingUser GetUSER(string email)
		{            
			try
			{
				ShoppingUserDAO userDAO = new ShoppingUserDAO();
				return userDAO.GetShoppingUserGetByEmail(email);                
			}
			catch (BusinessException)
			{
				throw;
			}
			catch (Exception ex)
			{
				// log this exception
				Logger.Error(ex);
				// wrap it and rethrow
				throw new BusinessException(SR.BusinessGetUSERException, ex);
			}
		}  

		public static int CreateShoppingUser(ShoppingUser user)
		{            
			try
			{
				ShoppingUserDAO userDAO = new ShoppingUserDAO();
				userDAO.CreateShoppingUser(user);
			}
			catch (BusinessException)
			{
				throw;
			}
			catch (Exception ex)
			{
				// log this exception
				Logger.Error(ex);
				// wrap it and rethrow
				throw new BusinessException(SR.BusinessCreateShoppingUserException, ex);
			}
			return user.UserId;
		}        

		public static void UpdateShoppingUser(ShoppingUser user)
		{            
			try
			{
				ShoppingUserDAO userDAO = new ShoppingUserDAO();
				userDAO.UpdateShoppingUser(user);
			}
			catch (BusinessException)
			{
				throw;
			}
			catch (Exception ex)
			{
				// log this exception
				Logger.Error(ex);
				// wrap it and rethrow
				throw new BusinessException(SR.BusinessUpdateShoppingUserException, ex);
			}
		}        

		#endregion

		#region "Shopping Customer"

		public static int CreateShoppingCustomer(ShoppingCustomer customer)
		{            
			try
			{
				ShoppingCustomerDAO customerDAO = new ShoppingCustomerDAO();
				customerDAO.CreateShoppingCustomer(customer);
			}
			catch (BusinessException)
			{
				throw;
			}
			catch (Exception ex)
			{
				// log this exception
				Logger.Error(ex);
				// wrap it and rethrow
				throw new BusinessException("BusinessCreateShoppingCustomerException", ex);
			}
			return customer.CustomerID;
		}        

		public static void UpdateShoppingCustomer(ShoppingCustomer customer)
		{            
			try
			{
				ShoppingCustomerDAO customerDAO = new ShoppingCustomerDAO();
				customerDAO.UpdateShoppingCustomer(customer);
			}
			catch (BusinessException)
			{
				throw;
			}
			catch (Exception ex)
			{
				// log this exception
				Logger.Error(ex);
				// wrap it and rethrow
				throw new BusinessException("BusinessUpdateShoppingCustomerException", ex);
			}
		}        

		public static void DeleteShoppingCustomer(int customerId)
		{            
			try
			{
				ShoppingCustomerDAO customerDAO = new ShoppingCustomerDAO();
				customerDAO.DeleteShoppingCustomer(customerId);
			}
			catch (BusinessException)
			{
				throw;
			}
			catch (Exception ex)
			{
				// log this exception
				Logger.Error(ex);
				// wrap it and rethrow
				throw new BusinessException("BusinessDeleteShoppingCustomerException", ex);
			}
		}  


		#endregion

	}
}
